package com.prs.crm.interceptor;

import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.StrutsResultSupport;

import com.opensymphony.xwork2.ActionChainResult;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.DefaultActionInvocation;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.interceptor.PreResultListener;
import com.opensymphony.xwork2.util.TextParseUtil;
import com.prs.crm.annotation.Clew;

public class ResultListener implements PreResultListener {

	protected String conditionalParse(String param,
			ActionInvocation invocation, final boolean encode) {
		if (param != null && invocation != null) {
			return TextParseUtil.translateVariables(param, invocation
					.getStack(), new TextParseUtil.ParsedValueEvaluator() {
				public Object evaluate(Object parsedValue) {
					if (encode) {
						if (parsedValue != null) {
							try {
								// use UTF-8 as this is the recommended encoding
								// by W3C to
								// avoid incompatibilities.
								return URLEncoder.encode(
										parsedValue.toString(), "UTF-8");
							} catch (UnsupportedEncodingException e) {
							}
						}
					}
					return parsedValue;
				}
			});
		} else {
			return param;
		}
	}

	public void beforeResult(ActionInvocation invocation, String resultCode) {
		try {
			Object action = invocation.getAction();
			String methodName = invocation.getProxy().getMethod();
			Method method = action.getClass().getMethod(methodName, null);
			if (method != null) {
				Annotation annotation = method.getAnnotation(Clew.class);// 获得当前方法的注释信息

				if (annotation != null) {
					for (String specresult : ((Clew) annotation).results()) {
						if (resultCode.equals(specresult)) {
							invocation.setResultCode(resultCode);

							Result result = ((DefaultActionInvocation) invocation)
									.createResult();
							invocation.setResultCode("clew");
							HttpServletRequest request = (HttpServletRequest) invocation
									.getInvocationContext().getContext().get(
											ServletActionContext.HTTP_REQUEST);
							String finalLocation="";
							try {
								finalLocation= conditionalParse(
											((StrutsResultSupport) result)
													.getLocation(), invocation, false);
							} catch (Exception e) {
								try {
									finalLocation= conditionalParse(
											((ActionChainResult) result).getChainHistory().getFirst()
													, invocation, false);
								} catch (Exception e2) {
									// TODO: handle exception
								}
								
							}
							
							invocation.getInvocationContext().getValueStack()
									.set("originResult", resultCode);
							request
									.setAttribute("clew-redirect",
											finalLocation);

						}
					}

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}